const g_data=getApp().globalData;
const util=require('../../utils/util.js');
Page({
  data: {
    real_name: '1',
    phone:'17785407973'
  },
  onLoad: function (options) {
    console.log(g_data)
  },
  input_name:function(e){
    this.setData({
      real_name : e.detail.value
    })  
  },
  input_phone:function(e){
    this.setData({
      phone: e.detail.value
    })  
  },
  submit:function(e){
    if (this.data.real_name==null){
      wx.showToast({
        title: '请输入您的真实姓名',
      })
      return;
    }
    if(this.data.phone==null){
      wx.showToast({
        title: '请输入您的联系电话',
      })
      return;
    }
    if (!check_phone_number(this.data.phone)){
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return;
    }
    let url ='/app/on_bind';
    let data={
      real_name:this.data.real_name,
      phone:this.data.phone,
      open_id: wx.getStorageSync('open_id'),
      nick_name: g_data.nick_name,
      head_img_url: g_data.head_img_url,
    };
    util.request(url,data,res=>{
      console.log(url,data)
      console.log(url,res)
      if(res.ret==0){
        wx.showModal({
          title: '恭喜您',
          content: '绑定成功',
          success:function(e){
            wx.redirectTo({
              url: '/pages/index/index',
            })
          },
          showCancel:false,
        })
      }
    })
  }

});

function check_phone_number(phone) {
  var myreg = /^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/;
  return myreg.test(phone);
}